#include <iostream>
#include <array>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    // 初始化一个包含5个元素的动态数组,每个元素的值为0
    vector<int> vs;
    
    // 如果实例化vector容器对象没有设置元素个数，则容器为空
    if(vs.empty())
    {
        cout<<"容器为空"<<endl;
    }else{
        cout<<"容器不为空"<<endl;
    }

    int size;
    cout<<"请输入要添加的元素个数:"<<endl;
    cin>>size;

    srand(time(0));
    for (int i = 0; i < size; i++)
    {
        vs.push_back(rand() % 100 );
    }
    for(vector<int>::iterator it = vs.begin(); it != vs.end(); it++)
    {
        cout<<*it<<" ";
    }
    cout<<endl;

    // 对vector容器中的元素进行排序
    std::sort(vs.begin(), vs.end());

    for(vector<int>::iterator it = vs.begin(); it != vs.end(); it++)
    {
        cout<<*it<<" ";
    }
    cout<<endl;


    int index;
    while (1)
    {
        cout<<"请输入要访问的数组下标:"<<endl;
        
        cin>>index;
        try
        {
            int value = vs.at(index);
            cout<<"该下标对应的值为:"<<value<<endl;
            break;
        }
        catch(const std::exception& e)
        {
             cout << "会越界，无法访问" << "请重新输入"<< endl;
                continue;
        }
        
    }
    



}